package com.tl.sun.ui.dialog;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.tl.sun.R;
import com.tl.sun.manager.MDConstant;
import com.tl.sun.model.ExceedUserEnetity;
import com.tl.sun.ui.activity.LoginActivity;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/4 10:30
 * 邮箱：hqy_xz@126.com
 */

public class TerminalDialogFragment extends DialogFragment {

    private static final String EXCEEEDUSER = "exceed_user";
    private static final String DIALOGTYPE = "dialog_type";
    private static final String CLOSETYPE = "close_type";
    @Bind(R.id.tv_terminal_coil)
    TextView mTvTerminalCoil;
    @Bind(R.id.tv_terminal_renewal)
    TextView mTvTerminalRenewal;

    private List<ExceedUserEnetity> list;
    private boolean isTerminal = false;
    private String dialogType;

    private String closeType;

    public static TerminalDialogFragment newInstance(List<ExceedUserEnetity> exceedUserEnetityList, String type, String close) {
        TerminalDialogFragment dialog = new TerminalDialogFragment();
        Bundle args = new Bundle();
        args.putSerializable(EXCEEEDUSER, (Serializable) exceedUserEnetityList);
        args.putString(DIALOGTYPE, type);
        args.putString(CLOSETYPE, close);
        dialog.setArguments(args);
        return dialog;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list = (List<ExceedUserEnetity>) getArguments().getSerializable(EXCEEEDUSER);
        dialogType = getArguments().getString(DIALOGTYPE);
        closeType = getArguments().getString(CLOSETYPE);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.Dialog_Tips);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_terminal, null);
        builder.setView(view);
        builder.setCancelable(false);
        ButterKnife.bind(this, view);

        return builder.create();
    }


    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.CENTER;
        wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(wlp);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        if (!isTerminal&&closeType.equals("1")) {
            Intent splash = new Intent(getActivity(), LoginActivity.class);
            startActivity(splash);
            getActivity().finish();
        }
    }

    @OnClick({R.id.tv_terminal_coil, R.id.tv_terminal_renewal})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_terminal_coil:
                new HintDialog(getActivity(), dialogType, list, MDConstant.DIALOG_FLAG_LOGIN_CONFLICT,closeType);
                isTerminal = true;
                dismiss();
                break;
            case R.id.tv_terminal_renewal:
                isTerminal = false;
                dismiss();
                break;
        }
    }
}
